package org.itboys.shop.tools;

import javax.servlet.http.HttpServletResponse;

import org.itboys.weixin.tools.HttpRequestUtils;


public class WeixinUserInfoUtils {

	/**
	 * 获取用户信息
	 * @param appId
	 * @param appSecret
	 * @param grant_type
	 * @param code
	 * @param state
	 * @param response
	 * @return
	 */
	public static String getUserInfo(String appId, String appSecret,String grant_type, String code,String state,
			HttpServletResponse response){
		String access_token = "";
		String openid = "";
		String result = "";
		try{
			String param1 = "appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=" + grant_type;
			//JSONObject jsonObject1 = new JSONObject(HttpRequestUtils.sendGet("https://api.weixin.qq.com/sns/oauth2/access_token", param1));
			/*if (jsonObject1.get("errcode") == "40029"){
				response.sendRedirect(state);//重新请求
			}
			access_token = String.valueOf(jsonObject1.get("access_token"));
			openid = String.valueOf(jsonObject1.get("openid"));*/
			String param2 = "access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
			result = HttpRequestUtils.sendGet("https://api.weixin.qq.com/sns/userinfo", param2);

		} catch (Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
}
